﻿using BootstrapBlazor.Components;
using Mapster;
using Microsoft.AspNetCore.Components;
using OnWork.DataCenter.BLL.DataCenter;
using OnWork.DataCenter.BLL.Standard.HostedService;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Shared.Common;

namespace OnWork.DataCenter.Shared.Pages.DataSource
{
    /// <summary>
    /// 主题配置
    /// </summary>
    public partial class MqttTopicView : ComponentBaseDataCenter
    {
        /// <summary>
        /// 依赖注入 提示信息
        /// </summary>
        [Inject]
        private DSMqttTopicBLL dBConnBLL { get; set; }

        /// <summary>
        /// 后台服务
        /// </summary>
        [Inject]
        private MqttHostedService mqttHostedService { get; set; }

        /// <summary>
        /// Mqtt 列表项目
        /// </summary>
        private IEnumerable<SelectedItem> MqttItems { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="firstRender"></param>
        protected async override void OnAfterRender(bool firstRender)
        {
            base.OnAfterRender(firstRender);
            if (firstRender)
            {
                var result = await dBConnBLL.QueryALLMqttAsync();
                if (result != null)
                {
                    MqttItems = result.Select(x => new SelectedItem { Value = x.ID.ToString(), Text = x.Name }).ToList();
                }
            }
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task<QueryData<MqttTopicVO>> QueryAsync(QueryPageOptions options)
        {
            var result = await dBConnBLL.QueryPagedListAsync(options.ToBaseQueryPageInput());
            //对象转换
            return result.ToQueryData<MqttTopicVO, DSMqttTopic>();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private async Task<bool> OnDeleteAsync(IEnumerable<MqttTopicVO> item)
        {
            var result = await dBConnBLL.DeleteDataAsync(item.Select(x => x.ID).ToArray());
            await MqttHostedServiceUpdate();
            return await ShowSqlReturnMsg(result);
        }

        //保存数据时
        private async Task<bool> OnSaveAsync(MqttTopicVO item, ItemChangedType changedType)
        {
            //RESTfulResult<object> result;
            if (changedType == ItemChangedType.Update) // 更新
            {
                var result = await dBConnBLL.UpdateDataAsync(item.Adapt<DSMqttTopic>());
                await MqttHostedServiceUpdate();
                return await ShowSqlReturnMsg(result);
            }
            else //新增
            {
                var result = await dBConnBLL.AddDataAsync(item.Adapt<DSMqttTopic>());
                await MqttHostedServiceUpdate();
                return await ShowSqlReturnMsg(result);
            }
        }

        /// <summary>
        /// 刷新后台服务
        /// </summary>
        /// <returns></returns>
        private async Task MqttHostedServiceUpdate()
        {
            await mqttHostedService.UpdateMqtt();
        }

        /// <summary>
        /// 连接测试
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private async Task OnConnTestAsync(MqttTopicVO item)
        {
            await DialogService.Show(new DialogOption()
            {
                Title = item.Name + "连接测试",
                BodyContext = item,
                BodyTemplate = builder =>
                {
                    var index = 0;
                    builder.OpenComponent<MqttTopicTestView>(index++);
                    builder.CloseComponent();
                }
            });
            //await base.ToastService?.Success("测试", item.ID.ToString());
        }
    }
}